package com.x.component.widget;

import com.x.component.connectivity.FrameworkServicesModule;

import android.app.AppGlobals;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.display.DisplayManager;
import android.os.SystemProperties;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

public class DisplayWidget {

	private static DisplayWidget mInstance = null;

	private static final Object mLock = new Object();

	private DisplayManager displayManager;

	private DisplayWidget() {

	}

	public static DisplayWidget getInstance() {
		if (null == mInstance) {
			synchronized (mLock) {
				if (null == mInstance) {
					mInstance = new DisplayWidget();
				}
			}
		}
		return mInstance;
	}

	public DisplayMetrics getDisplayMetric() {
		final Context context = AppGlobals.getInitialApplication();
		displayManager = FrameworkServicesModule.provideDisplayManager(context);
		DisplayMetrics metric = context.getResources().getDisplayMetrics();
		Configuration config = context.getResources().getConfiguration();
		Log.d("TinkerDisplay", "orientation: " + config.orientation);
		try {
			int width = SystemProperties.getInt("persist.panel.width", 1920);
			int height = SystemProperties.getInt("persist.panel.height", 720);
			displayManager.getDisplay(2).getMetrics(metric);
			metric.density = 1.875f;
			metric.densityDpi = 300;
			metric.scaledDensity = 2.4375f;
			Log.d("TinkerDisplay", "width: " + width);
			Log.d("TinkerDisplay", "height: " + height);

			Log.d("TinkerDisplay", "widthPixels: " + metric.widthPixels);
			Log.d("TinkerDisplay", "heightPixels: " + metric.heightPixels);

//			metric.widthPixels = width;
//			metric.heightPixels = height;

		} catch (Exception e) {
			Log.d("TinkerDisplay", "exception: " + e.getMessage());
		}
		return metric;
	}

	public Display getDisplay() {
		final Context context = AppGlobals.getInitialApplication();
		displayManager = FrameworkServicesModule.provideDisplayManager(context);
		Configuration config = context.getResources().getConfiguration();
		Log.d("TinkerDisplay", "orientation: " + config.orientation);
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		try {
			display = displayManager.getDisplay(2);
		} catch (Exception e) {
			Log.d("TinkerDisplay", "exception: " + e.getMessage());
		}
		if(display==null) {
			 display = wm.getDefaultDisplay();	
		}
		return display;
	}

	public Configuration getConfiguration() {
		final Context context = AppGlobals.getInitialApplication();
		Configuration config = context.getResources().getConfiguration();
//		DisplayMetrics metric = context.getResources().getDisplayMetrics();
		displayManager = FrameworkServicesModule.provideDisplayManager(context);

//		try {
//			displayManager.getDisplay(2).getMetrics(metric);
//		} catch (Exception e) {
//			Log.d("TinkerDisplay", "getConfiguration exception: " + e.getMessage());
//		}
//		metric.density = 1.875f;
//		metric.densityDpi = 300;
//		metric.scaledDensity = 2.4375f;

		Log.d("TinkerDisplay", "orientation: " + config.orientation);
		int orientation = SystemProperties.getInt("persist.panel.orientation", 0); // 0横屏，90竖屏
		Log.d("TinkerDisplay", "protocol orientation: " + orientation);
		config.orientation = orientation;
//		config.fontScale = metric.scaledDensity;
//		config.densityDpi = metric.densityDpi;
		return config;
	}

	public int getSystemProperties(String key) {
		return SystemProperties.getInt(key, 0);
	}
}
